package Controler.Planning;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.DateFormatter;

import JDBC.Cours_Afficher;
import JDBC.Eleve_Afficher;
import JDBC.Matiere_Afficher;
import Model.Cours;
import Model.Eleve;
import Model.Matiere;
import Model.Session;
import Model.StringSession;

/**
 * Servlet implementation class ShowPlanning
 */
@WebServlet("/ShowPlanning")
public class ShowPlanning extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowPlanning() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// On recupere l'id du professeur, et on demande � la base une liste de "Sessions".
		
		Cours_Afficher listeCours = new Cours_Afficher();
		ArrayList<Cours> mesCours = listeCours.afficherCours();
		
	
		Date HorraireDebut = new GregorianCalendar(2013, 2, 28, 15, 30).getTime();
		Date HorraireFin= new GregorianCalendar(2013, 2, 28, 17, 30).getTime();
		
		Date HorraireDebut2 = new GregorianCalendar(2013, 2, 26, 15, 30).getTime();
		Date HorraireFin2= new GregorianCalendar(2013, 2, 26, 17, 30).getTime();
		
		
		Session session1 = new Session(1,mesCours.get(0), 1, HorraireDebut, HorraireFin);
		Session session2 = new Session(2,mesCours.get(0), 1, HorraireDebut2, HorraireFin2);
		
		ArrayList<Session> sessions = new ArrayList<Session>();
		sessions.add(session1);
		sessions.add(session2);
		
		ArrayList<StringSession> StringSessions = new ArrayList<StringSession>();
		
		for(int i = 0; i < sessions.size(); i++){
			StringSessions.add(getStringSessions(sessions.get(i)));
		}
		
		System.out.println(sessions.size());
		System.out.println(StringSessions.size());
		
		request.setAttribute("Sessions", StringSessions);
		request.getRequestDispatcher("Home.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	public StringSession getStringSessions(Session session ){
		
		Cours cours = session.getCours();
		
		String eleve = "";
		Eleve_Afficher listeEleve = new Eleve_Afficher();
		ArrayList<Eleve> mesEleves = listeEleve.afficher();
		eleve = mesEleves.get(0).getPrenom() + " " + mesEleves.get(0).getNom();
		
		
		String matiere = cours.getMatiere();
		String niveau = cours.getNiveau();
		String lieu = cours.getVille();
		
		Calendar cal = new GregorianCalendar();
		cal.setTime(session.getDate_debut());

		int d = cal.get(Calendar.DAY_OF_MONTH);
		int m = cal.get(Calendar.MONTH) - 1;
		int y = cal.get(Calendar.YEAR);
		int h = cal.get(Calendar.HOUR_OF_DAY);
		int min = cal.get(Calendar.MINUTE);
		
		
		String dateDebut = "new Date("+y+","+m+"," + d + "," + String.valueOf(h) + "," + String.valueOf(min) + ")";
		
		Calendar cal2 = new GregorianCalendar();
		cal.setTime(session.getDate_fin());

		int d2 = cal2.get(Calendar.DAY_OF_MONTH);
		int m2 = cal2.get(Calendar.MONTH) - 1;
		int y2 = cal2.get(Calendar.YEAR);
		int h2= cal2.get(Calendar.HOUR_OF_DAY);
		int min2 = cal2.get(Calendar.MINUTE);
		
		
		
		String dateFin = "new Date("+y2+","+m2+"," + d2 + "," + String.valueOf(h2) + "," + String.valueOf(min2) + ")";
		return new StringSession(session.getId(), session.getId_serie(), eleve, matiere, niveau, lieu, dateDebut, dateFin);
		
	}
	
	

}

/*  Model/Controler TODO */

			/*  GET ELEVE BY ID  */ 
			/*  CREER SESSION DANS BASE */
			/*  Reflechir � la gestion des series (reflechir) */
			/*  GET ALL SESSIONS By ID Prof*/
			/*  ADD SESSION */ 
			/*  MODIFIER SESSION */ 
			/*  SUPPRIMER SESSION */
			/*  CREER SESSION DE FACON REPETE (reflechir) */

/* Vue tweak TODO */

//!check	/* Adapter vue */
			/* Vue cr�ation     session super complexe  (reflechir)*/
			/* Vue Modification session super complexe  (reflechir)*/
			/* Filtrer calendrier par eleves / matiere */
			/* Couleur par eleve */

/* BONUS : MODULE STATS */

			/* Nombres de cours par periode / ville / prof / matiere etc ...*/